package com.serviceImpl;

import com.javabean.Users;
import com.mapper.UserMapper;
import com.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @Author: 张波 
 * @Time: 2025/8/28 12:16
 */
@Service
public class UserServiceImpl implements UsersService {

	private final UserMapper userMapper;
	@Autowired
	public UserServiceImpl(UserMapper userMapper) {
		this.userMapper = userMapper;
	}
	/**
	 *
	 * @param username 用户名
	 * @param password 密码
	 * @return  返回Users对象
	 */
	@Override
	public Users Login(String username, String password) {
		return userMapper.selectByUserNameAndPassword(username,password);
	}

	/**
	 *
	 * @return 返回用户对象所有信息
	 */
	@Override
	public List<Users> findAll() {
		return userMapper.selectAll();
	}

	/**
	 *
	 * @param id 传入要修改的id
	 * @return 返回对象一个
	 */
	@Override
	public Users findById(int id) {
		return userMapper.selectById(id);
	}

	/**
	 *
	 * @param id 传入要删除的id
	 */
	@Override
	public void delById(int id) {
		userMapper.deleteById(id);
	}

	/**
	 *
	 * @param users
	 */
	@Transactional
	@Override
	public void add(Users users,int roleId) {
		userMapper.insertUsers(users);
		userMapper.insertU_R(users.getId(), roleId);
	}

	/**
	 *
	 * @param id
	 * @return
	 */
	@Override
	public Users findById2(int id) {
		return userMapper.selectById2(id);
	}

	/**
	 *
	 * @param users
	 * @param roleId
	 */
	@Transactional
	@Override
	public void edit(Users users, int roleId) {
		userMapper.updateUsers(users);
		userMapper.updateU_R(users.getId(), roleId);
	}
}
